﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GameClient;
using UnityEngine.UI;
using Protocol.DTO;
using Protocol;

public class LoginHandler : MonoBehaviour ,IMessageHandle{
    public InputField u_name, u_password;//登陆界面使用
    public InputField u_name_r, u_password1_r, u_password2_r;//注册界面使用
    public GameObject ResWin;//注册界面
    public Button LoginBtn, ResBtn;
	// Use this for initialization
	void Start () {
        ResWin.SetActive(false);
        LoginBtn.onClick.AddListener(LoginBtnClick);
        ResBtn.onClick.AddListener(ResBtnClick);
	}
	
	// Update is called once per frame
	void Update () {
		
	}

    public void LoginBtnClick()
    {
        LoginBtn.interactable = false;
        //获取输入文本框中的内容
        string name_tx = u_name.text;
        string password_tx = u_password.text;
        //todo 校验
        if(name_tx.Equals("")||password_tx.Equals(""))
        {
            Debug.Log("输入不能为空");
            LoginBtn.interactable = true;
            return;
        }
        //发送登陆请求
        User_DTO dto = new User_DTO();
        dto.username = name_tx;
        dto.password = password_tx;
        this.SendModel(new TransModel(GameProtocol.login, 0, LoginProtocol.Login_CREQ, dto));
    }
    /// <summary>
    /// 登陆界面的注册按钮
    /// </summary>
    public void ResWinBtnClick()
    {
        ResWin.SetActive(true);
    }
    public void ResBtnClick()
    {
        ResBtn.interactable = false;
        //获取输入文本框中的内容
        string name_tx = u_name_r.text;
        string password_tx1 = u_password1_r.text;
        string password_tx2 = u_password2_r.text;
        //todo 校验
        if (name_tx.Equals("") || password_tx1.Equals("") || password_tx2.Equals(""))
        {
            Debug.Log("输入不能为空");
            ResBtn.interactable = true;
            return;
        }
        if(!password_tx1.Equals(password_tx2))
        {
            Debug.Log("两次密码不相同");
            ResBtn.interactable = true;
            return;
        }
        //发送登陆请求
        User_DTO dto = new User_DTO();
        dto.username = name_tx;
        dto.password = password_tx1;
        this.SendModel(new TransModel(GameProtocol.login, 0, LoginProtocol.Reg_CREQ, dto));
    }

    public void OnMessageReceive(TransModel model)
    {
        if (model == null)
            return;
        switch (model.command)
        {
            case LoginProtocol.Login_SRES:
                LoginResult(model.area);
                break;
            case LoginProtocol.Reg_SRES:
                ResResult(model.area);
                break;
            default:
                break;
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="result">
    /// 0 登陆成功
    /// 1 账号不存在
    /// 2 账号已上线
    /// 3 密码不正确
    /// </param>
    void LoginResult(int result)
    {
        LoginBtn.interactable = true;
        switch (result)
        {
            case 0:
                Debug.Log("登陆成功");
                break;
            case 1:
                Debug.Log("账号不存在");
                break;
            case 2:
                Debug.Log("账号已上线");
                break;
            case 3:
                Debug.Log("密码不正确");
                break;
            default:
                break;
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="result">
    /// 0 注册成功
    /// 1 账号已存在
    /// </param>
    void ResResult(int result)
    {
        ResBtn.interactable = true;
        switch (result)
        {
            case 0:
                Debug.Log("注册成功");
                ResWin.SetActive(false);
                break;
            case 1:
                Debug.Log("账号已存在");
                break;
            default:
                break;
        }
    }
}
